package com.example.myapplication;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;



public class FileStorageActivity extends AppCompatActivity {

    final String FileName = "No.39xwhhp.txt";
    final int __FileBufferSize = 1024;
    boolean ischanged = false;

    ImageView openFileBtn, saveFileBtn, exitFileBtn;
    EditText editFileEdt, filePathEdt;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_file_storage);


        openFileBtn = findViewById(R.id.openFileBnt);
        saveFileBtn = findViewById(R.id.saveFileBnt);
        exitFileBtn = findViewById(R.id.exitFileBnt);
        editFileEdt = findViewById(R.id.editFileEdt);
        filePathEdt = findViewById(R.id.filePathEdt);

        openFileBtn.setOnClickListener(aa);
        saveFileBtn.setOnClickListener(aa);
        exitFileBtn.setOnClickListener(aa);

        editFileEdt.addTextChangedListener(tc);

    }

    private View.OnClickListener aa = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.openFileBnt:
                    __ReadFile();
                    break;
                case R.id.saveFileBnt:
                    __WriteFile();
                    break;
                case R.id.exitFileBnt:
                    if(ischanged){
                        AlertDialog.Builder ad = new AlertDialog.Builder(FileStorageActivity.this);
                        ad.setTitle("tip").setMessage("file has changed, wanna save?")
                                .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        __WriteFile();
                                        finish();
                                    }
                                })
                                .setNegativeButton("No", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        finish();
                                    }
                                }).create().show();

                    }else{
                        finish();
                        Toast.makeText(FileStorageActivity.this, "Exited", Toast.LENGTH_SHORT).show();
                    }
            }
        }
    };

    private  void __ReadFile(){
        try{
            FileInputStream fis = openFileInput(FileName);

            byte[] buff = new byte[this.__FileBufferSize];

            int hasRead = 0;
            StringBuilder sb = new StringBuilder();
            while((hasRead = fis.read(buff)) > 0){
                sb.append(new String(buff, 0, hasRead));
            }
            fis.close();
            editFileEdt.setText(sb);
            filePathEdt.setText(getFilesDir().toString());
        }catch(FileNotFoundException e){
            e.printStackTrace();
        }catch(IOException e){
            e.printStackTrace();
        }
    }

    private void __WriteFile(){

        String ss = editFileEdt.getText().toString();
        try {
            FileOutputStream fos = openFileOutput(FileName, Context.MODE_PRIVATE);

            fos.write(ss.getBytes());
            fos.close();
            ischanged = false;
        }catch(FileNotFoundException e){
            e.printStackTrace();
        }catch(IOException e ){
            e.printStackTrace();
        }

    }

    private TextWatcher tc = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            ischanged = true;
        }
    };
}